package net.ddns.xcyyds.kungfu.procedures;

import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.event.TickEvent;

import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.network.chat.TextComponent;

import net.ddns.xcyyds.kungfu.network.KungFuModVariables;

import javax.annotation.Nullable;

@Mod.EventBusSubscriber
public class PlayerTickProcedure {
	@SubscribeEvent
	public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
		if (event.phase == TickEvent.Phase.END) {
			execute(event, event.player);
		}
	}

	public static void execute(Entity entity) {
		execute(null, entity);
	}

	private static void execute(@Nullable Event event, Entity entity) {
		if (entity == null)
			return;
		if ((entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new KungFuModVariables.PlayerVariables())).howToUse
				&& entity.getPersistentData().getBoolean("kungFuUse")) {
			entity.getPersistentData().putDouble("tickTime2", (entity.getPersistentData().getDouble("tickTime2") + 1));
			if (entity.getPersistentData().getDouble("tickTime2") >= 10) {
				entity.getPersistentData().putDouble("tickTime2", 1);
				if (!((entity.getPersistentData().getString("NO1")).equals("R") || (entity.getPersistentData().getString("NO1")).equals("L")
						|| (entity.getPersistentData().getString("NO1")).equals("_"))) {
					entity.getPersistentData().putString("NO1", "_");
				}
				if (!((entity.getPersistentData().getString("NO2")).equals("R") || (entity.getPersistentData().getString("NO2")).equals("L")
						|| (entity.getPersistentData().getString("NO2")).equals("_"))) {
					entity.getPersistentData().putString("NO2", "_");
				}
				if (!((entity.getPersistentData().getString("NO3")).equals("R") || (entity.getPersistentData().getString("NO3")).equals("L")
						|| (entity.getPersistentData().getString("NO3")).equals("_"))) {
					entity.getPersistentData().putString("NO3", "_");
				}
				if (entity instanceof Player _player && !_player.level.isClientSide())
					_player.displayClientMessage(new TextComponent(("-" + entity.getPersistentData().getString("NO1") + "-"
							+ entity.getPersistentData().getString("NO2") + "-" + entity.getPersistentData().getString("NO3") + "-")), (true));
			}
		}
		if (entity.getPersistentData().getDouble("lookMeGUI") > 0) {
			entity.getPersistentData().putDouble("lookMeGUI", (entity.getPersistentData().getDouble("lookMeGUI") - 0.04));
		}
		entity.getPersistentData().putString("lookMe1", "\u00A7l\u4E2A\u4EBA\u5C5E\u6027");
		entity.getPersistentData().putString("lookMe2", ("\u00A7l\u5185\u529B\uFF1A" + new java.text.DecimalFormat("##").format(
				(entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new KungFuModVariables.PlayerVariables())).neiLi)
				+ "/" + new java.text.DecimalFormat("##").format((entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
						.orElse(new KungFuModVariables.PlayerVariables())).neiLiMax)));
		entity.getPersistentData().putString("lookMe3",
				("\u00A7l\u5355\u624B\u5251\u672F\u719F\u7EC3\u5EA6\uFF1A"
						+ new java.text.DecimalFormat("##.#").format((entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new KungFuModVariables.PlayerVariables())).oneHandJian)));
		entity.getPersistentData().putString("lookMe4",
				("\u00A7l\u62F3\u6CD5\u719F\u7EC3\u5EA6\uFF1A"
						+ new java.text.DecimalFormat("##.#").format((entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new KungFuModVariables.PlayerVariables())).quanFa)));
		if (entity.getPersistentData().getDouble("JiuShui") > 0) {
			entity.getPersistentData().putDouble("JiuShui", (entity.getPersistentData().getDouble("JiuShui") - 0.001));
		}
		if (entity.getPersistentData().getDouble("JiuShui") > 5) {
			if (entity instanceof LivingEntity _entity)
				_entity.addEffect(new MobEffectInstance(MobEffects.DAMAGE_BOOST, 200, 0, (false), (false)));
			if (entity instanceof LivingEntity _entity)
				_entity.addEffect(new MobEffectInstance(MobEffects.CONFUSION, 200, 2, (false), (false)));
		}
	}
}
